<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class ProductTimelines extends Model {

    protected $table ="product_timelines";

    protected $pk = 'product_timelines_id';


    /*
     * 获取total
     * */
    public function getTotal($where=[]){
        return Db::name('product_timelines')->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('product_timelines')->field("product_timelines_id")->where($where);


        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        $baseTimelines = new BaseTimelines();
        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $baseTimelines->getInfo($item['product_timelines_id']);
            }
            return $return;
        }
        return [];
    }


    /*
     * 获取商品售后服务属性列表
     * */
    public function getProductTimeline($product_id){
        if(!$product_id) return [];

        $where =[];
        $where[] = ["product_id","=",$product_id];
        $where[] = ["product_timelines_status","=",1];
        $info = Db::name('product_timelines')->field("timelines_id")->where($where)->find();
        return (new BaseTimelines())->getInfo($info['timelines_id']);
    }



    /*
    * 删除商品所有规格
    * */
    public function delProductTimelines($product_id){
        $where =[];
        $where[] = ["product_id","=",$product_id];
        return $this->delAll($where);
    }


    /*
     * 删除操作
     * */
    public function delAll($where){
        return Db::name('product_timelines')->where($where)->delete();
    }



    /*
* 删除指定商品的缓存
**/

    public function delProductTimelinesCache($product_id){
        $where =[];
        $where[] = ["product_id","=",$product_id];
        $list = Db::name('product_timelines')->field("product_timelines_id")->where($where)->select();
        if($list){
            $redisModel = new RedisModel();
            foreach($list as $item){
                $redisModel->delete($this->cacheKey.$item["product_timelines_id"]);
            }
        }
    }


    /*
   * 删除全部商品的缓存
   **/

    public function delAllProductTimelinesCache(){
        $redisModel = new RedisModel();
        $redisModel->deleteAll($this->cacheKey);
    }
}